package com.gzxw.mogublog.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gzxw.mogublog.admin.annotation.OperationLogger.OperationLogger;
import com.gzxw.mogublog.commons.entity.Blog;
import com.gzxw.mogublog.utils.R;
import com.gzxw.mogublog.xo.service.BlogService;
import com.gzxw.mogublog.xo.vo.BlogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;


    // 1.获取博客列表
    @PostMapping("/getList")
    public R getList(@RequestBody BlogVO blogVO) {

        IPage<Blog> pageList= blogService.getPageList(blogVO);

        Map<String ,Object> result = new HashMap<>();
        List<Blog> records = pageList.getRecords();
        long total = pageList.getTotal();
        long size = pageList.getSize();
        long current = pageList.getCurrent();
        result.put("records",records);
        result.put("total",total);
        result.put("size",size);
        result.put("current",current);
        return R.success("200",result);
    }


    // 2.添加博客
    @PostMapping("/addBlog")
    public R addBlog(@RequestBody BlogVO blogVO) {
        // TODO 解决重复提交的问题
       String message =  blogService.addBlog(blogVO);

        return R.success("200",message);
    }

    // 3.本地上传博客
    @PostMapping("/uploadLocalBlog")
    public R uploadLocalBlog(@RequestBody List<MultipartFile> files) {
        return null;
    }


    // 4.编辑博客
    @OperationLogger(value = "编辑博客")
    @PostMapping("/edit")
    public R edit(@RequestBody BlogVO blogVO) {

        String message = blogService.edit(blogVO);
        return R.success("200",message);
    }

    // 5.删除博客
    @PostMapping("/delete")
    public R delete(@RequestBody BlogVO blogVO) {

       String message =  blogService.deleteBlog(blogVO);

        return R.success("200",message);
    }

    // 5.删除选中博客
    public R deleteBatch() {


        return null;
    }
}
